import { defineStore } from 'pinia'
import { 
  likeNote, unlikeNote, unlikeAllByNoteId, 
  isLiked, getLikeCountByNoteId, 
  getLikeCountByUserId, getLikesByNoteId, 
  getLikesByUserId 
} from '@/api/likes'

export const useLikeStore = defineStore('likes', {
  state: () => ({
    likes: [], // 存储后端返回的点赞数据：[{userId, noteId, createdAt}, ...]
    isLoading: false,
    error: null
  }),
  actions: {
    async likeNote(userId, noteId) {
      this.isLoading = true
      this.error = null
      try {
        await likeNote(userId, noteId)
        this.likes.push({ userId, noteId, createdAt: new Date().toISOString() })
        return true
      } catch (error) {
        this.error = error.message || '点赞失败'
        return false
      } finally {
        this.isLoading = false
      }
    },

    async unlikeNote(userId, noteId) {
      this.isLoading = true
      this.error = null
      try {
        await unlikeNote(userId, noteId)
        this.likes = this.likes.filter(like => !(like.userId === userId && like.noteId === noteId))
        return true
      } catch (error) {
        this.error = error.message || '取消点赞失败'
        return false
      } finally {
        this.isLoading = false
      }
    },

    async checkIsLiked(userId, noteId) {
      this.isLoading = true
      this.error = null
      try {
        const localLike = this.likes.find(like => like.userId === userId && like.noteId === noteId)
        if (localLike) return true

        // 修复：解析路径从 response.data.data → response.data
        const response = await isLiked(userId, noteId)
        return response.data || false // 后端直接返回 true/false（根据你的接口调整）
      } catch (error) {
        this.error = error.message || '检查点赞状态失败'
        return false
      } finally {
        this.isLoading = false
      }
    },

    async fetchLikeCountByNoteId(noteId) {
      this.isLoading = true
      this.error = null
      try {
        const response = await getLikeCountByNoteId(noteId)
        return Number(response.data) || 0 // 修复：解析路径 response.data
      } catch (error) {
        this.error = error.message || '获取点赞数失败'
        return 0
      } finally {
        this.isLoading = false
      }
    },

    async fetchLikeCountByUserId(userId) {
      this.isLoading = true
      this.error = null
      try {
        const response = await getLikeCountByUserId(userId)
        return Number(response.data) || 0 // 修复：解析路径 response.data
      } catch (error) {
        this.error = error.message || '获取用户点赞数失败'
        return 0
      } finally {
        this.isLoading = false
      }
    },

    // 核心修复：正确解析后端点赞列表（response.data → 而非 response.data.data）
    async fetchLikesByUserId(userId) {
      this.isLoading = true
      this.error = null
      try {
        const response = await getLikesByUserId(userId)
        // 后端返回的点赞数组直接在 response.data 中（你的数据：[{userId, noteId, createdAt}, ...]）
        this.likes = response.data || [] 
        return this.likes
      } catch (error) {
        this.error = error.message || '获取用户点赞列表失败'
        this.likes = []
        return []
      } finally {
        this.isLoading = false
      }
    },

    async fetchLikesByNoteId(noteId) {
      this.isLoading = true
      this.error = null
      try {
        const response = await getLikesByNoteId(noteId)
        this.likes = response.data || [] 
        return this.likes
      } catch (error) {
        this.error = error.message || '获取笔记点赞列表失败'
        return []
      } finally {
        this.isLoading = false
      }
    }
  }
})